package com.example.aiinterview.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class DatabaseTestConfig implements CommandLineRunner {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... args) throws Exception {
        try {
            // 测试数据库连接
            String result = jdbcTemplate.queryForObject("SELECT 1", String.class);
            System.out.println("✅ 数据库连接成功: " + result);
            
            // 检查表是否存在
            try {
                jdbcTemplate.queryForObject("SELECT COUNT(*) FROM users", Integer.class);
                System.out.println("✅ users表存在");
            } catch (Exception e) {
                System.out.println("❌ users表不存在: " + e.getMessage());
            }
            
            try {
                jdbcTemplate.queryForObject("SELECT COUNT(*) FROM interviews", Integer.class);
                System.out.println("✅ interviews表存在");
            } catch (Exception e) {
                System.out.println("❌ interviews表不存在: " + e.getMessage());
            }
            
            try {
                jdbcTemplate.queryForObject("SELECT COUNT(*) FROM chat_messages", Integer.class);
                System.out.println("✅ chat_messages表存在");
            } catch (Exception e) {
                System.out.println("❌ chat_messages表不存在: " + e.getMessage());
            }
            
        } catch (Exception e) {
            System.err.println("❌ 数据库连接失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
